//
//  SLPrivateAndConditionsVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/10/7.
//

#import "SLPrivateAndConditionsVC.h"

@interface SLPrivateAndConditionsVC ()<WKNavigationDelegate,WKUIDelegate>{
    UIButton *_curBtn;
    NSString *_privateUrl;
    NSString *_conditionsUrl;
}
@property (nonatomic, strong)WKWebView *webView;
@end

@implementation SLPrivateAndConditionsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    _privateUrl = @"http://msl-stage.s3-website-us-east-1.amazonaws.com/#/appsupport/privacy/en/iOS";
    _conditionsUrl = @"http://www.baidu.com/msl_user/ConfidentialityAgreement";
    self.title = kLocalString(@"Terms & conditions");
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    [self setBtnSelectStyle:_leftBtn];
    [self setBtnSelectStyle:_rightBtn];
    _leftBtn.selected = YES;
    _rightBtn.selected = NO;
    _curBtn = _leftBtn;
    if (_curPage == 2) {
        _leftBtn.selected = NO;
        _rightBtn.selected = YES;
        _curBtn = _rightBtn;
    }
    _topView.layer.cornerRadius = 20;
    _topView.layer.masksToBounds = YES;
    [self updateBtnUI];
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectZero];
    [_bgView addSubview:_webView];
    _webView.backgroundColor = [UIColor clearColor];
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    _webView.navigationDelegate = self;
    [self loadUrl];
}

-(void)setBtnSelectStyle:(UIButton *)btn{
    // 创建渐变图层
    UIGraphicsBeginImageContextWithOptions(btn.bounds.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawLinearGradient(context,
                                CGGradientCreateWithColors(NULL,
                                                           (__bridge CFArrayRef)@[(id)KColor_O.CGColor, (id)KColor_Y.CGColor],
                                                           (CGFloat[]){0, 1}),
                                CGPointMake(0, 0),
                                CGPointMake(btn.bounds.size.width, 0),
                                kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [btn setTitleColor:KWhiteColor_text forState:UIControlStateNormal];
    [btn setTitleColor:KColor_1 forState:UIControlStateSelected];
    [btn setBackgroundImage:gradientImage forState:UIControlStateSelected];
}

- (IBAction)leftAction:(UIButton *)sender {
    if (_curBtn == sender) {
        return;
    }
    
    _curBtn.selected = NO;
    sender.selected = YES;
    _curBtn = sender;
    [self updateBtnUI];
    [self loadUrl];
}

-(void)loadUrl{
    NSString *url = _curBtn == _rightBtn ? _conditionsUrl :_privateUrl;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [self.webView loadRequest:request];
}


-(void)updateBtnUI{
    _leftBtn.layer.cornerRadius = 0;
    _rightBtn.layer.cornerRadius = 0;
    _curBtn.layer.cornerRadius = 20;
    _curBtn.layer.masksToBounds = YES;
}

@end
